package org.jeecg.modules.hotel.checkmix.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.hotel.checkmix.entity.Repeat;
import  org.jeecg.modules.hotel.checkmix.entity.TestB;
import org.jeecg.modules.hotel.checkmix.mapper.CheckMixMapper;
import org.jeecg.modules.hotel.checkmix.mapper.TestBMixMapper;
import org.jeecg.modules.hotel.checkmix.service.IcheckmixService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 重复住宿校验
 * @Author: jeecg-boot
 * @Date:   2021-04-26
 * @Version: V1.0
 */
@Service
public class checkmixServiceImpl extends ServiceImpl<CheckMixMapper, Repeat> implements IcheckmixService {

	@Autowired
	private CheckMixMapper checkMapper;
	@Autowired
	private TestBMixMapper testBMapper;
	
	@Override
	@Transactional
	public void saveMain(Repeat check, List<TestB> testBList) {
		checkMapper.insert(check);
		if(testBList!=null && testBList.size()>0) {
			for(TestB entity:testBList) {
				//外键设置
				entity.setRoomId(check.getId());
				testBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Repeat check, List<TestB> testBList) {
		checkMapper.updateById(check);
		
		//1.先删除子表数据
		testBMapper.deleteByMainId(check.getId());
		
		//2.子表数据重新插入
		if(testBList!=null && testBList.size()>0) {
			for(TestB entity:testBList) {
				//外键设置
				entity.setRoomId(check.getId());

				testBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		testBMapper.deleteByMainId(id);
		checkMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			testBMapper.deleteByMainId(id.toString());
			checkMapper.deleteById(id);
		}
	}
	/**
	 * 返回一个IPage对象 作为参数给页面 Result.Ok（Ipage）
	 * @param page
	 * @param queryWrapper
	 * @return
	 */


	@Override
	public IPage<Repeat> checkRepeatCw(Page<Repeat> page, QueryWrapper<Repeat> queryWrapper) {
		IPage<Repeat> RepeatPage = new Page<>();
		List<Repeat> repeatList = checkMapper.checkRepeatCw();
		RepeatPage.setRecords(repeatList);
		return RepeatPage;
	}

	@Override
	public List<Repeat> checkRepeatCwList(QueryWrapper<Repeat> queryWrapper) {

		List<Repeat> repeatList = checkMapper.checkRepeatCw();

		return repeatList;
	}


}
